﻿using System.Linq;
using MC.Warehouse.Entity;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace MC.Warehouse.Models
{
    public class CustomerModel : BaseModel
    {
        #region CONTRUCTOR
        
        public CustomerModel() 
        {
            Initialize();
        }

        #endregion

        #region PROPERTIES AND DATA FIELD

        private Customer _customer;

        public string Id
        {
            get
            {
                return _customer.Id;
            }
            set
            {
                _customer.Id = value;
                NotifyPropertyChanged(()=>Id);
            }
        }

        [DisplayName]
        public string Code
        {
            get
            {
                return _customer.Code;
            }
            set
            {
                _customer.Code = value;
                NotifyPropertyChanged(() => Code);
            }
        }

        public string ContactName
        {
            get
            {
                return _customer.ContactName;
            }
            set
            {
                _customer.ContactName = value;
                NotifyPropertyChanged(() => ContactName);
            }
        }

        public string CompanyName
        {
            get
            {
                return _customer.CompanyName;
            }
            set
            {
                _customer.CompanyName = value;
                NotifyPropertyChanged(() => CompanyName);
            }
        }

        public string Address
        {
            get
            {
                return _customer.Address;
            }
            set
            {
                _customer.Address = value;
                NotifyPropertyChanged(() => Address);
            }
        }

        public string Region
        {
            get
            {
                return _customer.Region;
            }
            set
            {
                _customer.Region = value;
                NotifyPropertyChanged(() => Region);
            }
        }

        public string Country
        {
            get
            {
                return _customer.Country;
            }
            set
            {
                _customer.Country = value;
                NotifyPropertyChanged(() => Country);
            }
        }

        public string Phone
        {
            get
            {
                return _customer.Phone;
            }
            set
            {
                _customer.Phone = value;
                NotifyPropertyChanged(() => Phone);
            }
        }

        public string Fax
        {
            get
            {
                return _customer.Fax;
            }
            set
            {
                _customer.Fax = value;
                NotifyPropertyChanged(() => Fax);
            }
        }

        public string Note
        {
            get
            {
                return _customer.Note;
            }
            set
            {
                _customer.Note = value;
                NotifyPropertyChanged(() => Note);
            }
        }

        public string Email
        {
            get
            {
                return _customer.Email;
            }
            set
            {
                _customer.Email = value;
                NotifyPropertyChanged(() => Email);
            }
        }

        #endregion
        
        #region IMLEMENT BASE MODEL

        const string Donotnullempty = "KO DC BO TRONG";
        const string Mailerror = "Mail Looix";
        const string Pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

        public override string[] ValidatedProperties { get; set; }

        public override sealed void Initialize()
        {
            _customer = new Customer();
            ValidatedProperties = new[] { 
                "Code", "ContactName", "CompanyName", "Address" ,
                "Region", "Country", "Phone", "Fax" ,"Email"
            };
        }
        
        public override string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Code":
                    error = string.IsNullOrEmpty(Code) ? Donotnullempty : null;
                    break;
                case "ContactName":
                    error = string.IsNullOrEmpty(ContactName) ? Donotnullempty : null;
                    break;
                case "CompanyName":
                    error = string.IsNullOrEmpty(CompanyName) ? Donotnullempty : null;
                    break;
                case "Address":
                    error = string.IsNullOrEmpty(Address) ? Donotnullempty : null;
                    break;
                case "Region":
                    error = string.IsNullOrEmpty(Region) ? Donotnullempty : null;
                    break;
                case "Country":
                    error = string.IsNullOrEmpty(Country) ? Donotnullempty : null;
                    break;
                case "Phone":
                    error = string.IsNullOrEmpty(Phone) ? Donotnullempty : null;
                    break;
                case "Fax":
                    error = string.IsNullOrEmpty(Fax) ? Donotnullempty : null;
                    break;
                case "Email":
                    if (string.IsNullOrEmpty(Email))
                        error = Donotnullempty;
                    else if (!Regex.IsMatch(Email, Pattern, RegexOptions.IgnoreCase))
                        error = Mailerror;
                    else return null;
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on Customer: " + propertyName);
                    break;
            }

            return error;
        }

        public new bool IsValid()
        {
            return ValidatedProperties.All(item => string.IsNullOrEmpty(GetValidationError(item)));
        }

        #endregion
    }
}
